/*
 * Created by SharpDevelop.
 * Author: Joey
 * Date: 25/09/2007
 * Time: 23:30
 * 
 * Source is copyright Joey Sabey 2007
 */

using System;
using System.Drawing;
using System.Windows.Forms;

namespace MonoTheism
{
	/// <summary>
	/// Description of AGNT_Properties.
	/// </summary>
	public partial class AGNT_Properties : Form
	{
		private bool bAcceptChanges = false;
		public bool AcceptChanges
		{
			get{return bAcceptChanges;}
		}
		public string AgentName
		{
			get{return txtName.Text;}
		}
		public AGNT_Block Agent
		{
			get
			{
				AGNT_Block agntBuffer = new AGNT_Block(txtName.Text);
				agntBuffer.BioenergyValue = System.Int32.Parse(txtBValue.Text);
				agntBuffer.RemoveScript = txtRemove.Text;
				agntBuffer.AnimFile = txtAnimFile.Text;
				agntBuffer.AnimGallery = txtAnimGallery.Text;
				agntBuffer.AnimString = txtAnimString.Text;
				foreach(TreeNode tn in treeDep.Nodes)
				{
					Dependency depBuffer = (Dependency)tn.Tag;
					agntBuffer.AddDependency(depBuffer.FileName,
					                         depBuffer.Category);
				}
				foreach(TreeNode tn in treeScripts.Nodes)
				{
					agntBuffer.AddScript((String)tn.Tag);
				}
				return agntBuffer;
			}
		}
		
		public AGNT_Properties()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
		}
		public AGNT_Properties(AGNT_Block agent)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			this.txtName.Text = agent.Name;
			this.txtBValue.Text = agent.BioenergyValue.ToString();
			this.txtAnimFile.Text = agent.AnimFile;
			this.txtAnimGallery.Text = agent.AnimGallery;
			this.txtAnimString.Text = agent.AnimString;
			this.txtRemove.Text = agent.RemoveScript;
			foreach(string s in agent.Scripts)
			{
				TreeNode tnBuffer = new TreeNode(System.IO.Path.GetFileName(s));
				tnBuffer.Tag = s;
				this.treeScripts.Nodes.Add(tnBuffer);
			}
			for(int x=0; x < agent.DependencyCount; x++)
			{
				TreeNode tnBuffer = new TreeNode(agent.Dependencies[x]);
				Dependency depBuffer = new Dependency(agent.Dependencies[x],
				                                      agent.DependencyCategories[x]);
				tnBuffer.Tag = depBuffer;
				this.treeDep.Nodes.Add(tnBuffer);
			}
		}
		
		void TsbAddDependencyClick(object sender, EventArgs e)
		{
			Dependency_Properties dpDialogue = new Dependency_Properties();
			dpDialogue.ShowDialog();
			if(dpDialogue.AcceptChanges)
			{
				TreeNode tnBuffer = new TreeNode(dpDialogue.FileName+
				                                 " ("+
				                                 dpDialogue.Category+
				                                 ")");
				Dependency dBuffer = new Dependency(dpDialogue.FileName,
				                                    dpDialogue.Category);
				tnBuffer.Tag = dBuffer;
				treeDep.Nodes.Add(tnBuffer);
			}
		}
		
		void TsbRemoveDependencyClick(object sender, EventArgs e)
		{
			try
			{
				treeDep.SelectedNode.Remove();
			}
			catch(NullReferenceException)
			{
				WarningDialogue warning = new WarningDialogue("Nothing was selected.");
				warning.ShowDialog();
			}
		}
		
		void TsbEditDependencyClick(object sender, EventArgs e)
		{
			try
			{
				Dependency_Properties dpDialogue = new Dependency_Properties((Dependency)treeDep.SelectedNode.Tag);
				dpDialogue.ShowDialog();
				if(dpDialogue.AcceptChanges)
				{
					Dependency dBuffer = new Dependency(dpDialogue.FileName,
				                                    dpDialogue.Category);
					treeDep.SelectedNode.Tag = dBuffer;
					treeDep.SelectedNode.Text = dpDialogue.FileName+
				                                 " ("+
				                                 dpDialogue.Category+
				                                 ")";
				}
			}
			catch(NullReferenceException)
			{
				WarningDialogue warning = new WarningDialogue("Nothing was selected.");
				warning.ShowDialog();
			}
		}
		
		void TsbAddScriptClick(object sender, EventArgs e)
		{
			Script_Properties spDialogue = new Script_Properties();
			spDialogue.ShowDialog();
			if(spDialogue.AcceptChanges)
			{
				TreeNode tnBuffer = new TreeNode(System.IO.Path.GetFileName(spDialogue.ScriptFile));
				tnBuffer.Tag = spDialogue.ScriptFile;
				treeScripts.Nodes.Add(tnBuffer);
			}
		}
		
		void TsbRemoveScriptClick(object sender, EventArgs e)
		{
			try
			{
				treeScripts.SelectedNode.Remove();
			}
			catch(NullReferenceException)
			{
				WarningDialogue warning = new WarningDialogue("Nothing was selected.");
				warning.ShowDialog();
			}
		}
		
		void TsbEditScriptClick(object sender, EventArgs e)
		{
			try
			{
				Script_Properties spDialogue = new Script_Properties((String)treeScripts.SelectedNode.Tag);
				spDialogue.ShowDialog();
				if(spDialogue.AcceptChanges)
				{
					treeScripts.SelectedNode.Tag = spDialogue.ScriptFile;
					treeScripts.SelectedNode.Text = System.IO.Path.GetFileName(spDialogue.ScriptFile);
				}
			}
			catch(NullReferenceException)
			{
				WarningDialogue warning = new WarningDialogue("Nothing was selected.");
				warning.ShowDialog();
			}
		}
		
		void TxtAnimFileTextChanged(object sender, EventArgs e)
		{
			this.txtAnimGallery.Text = System.IO.Path.GetFileNameWithoutExtension(txtAnimFile.Text);
		}
		
		void BtOkClick(object sender, EventArgs e)
		{
			bool bOk = true;
			//check fields
			if(txtName.Text.Length == 0)
			{
				bOk = false;
				WarningDialogue warning = new WarningDialogue("Agent Name field must not be empty");
				warning.ShowDialog();
			}
			if(txtAnimFile.Text.Length == 0)
			{
				bOk = false;
				WarningDialogue warning = new WarningDialogue("Animation File field must not be empty");
				warning.ShowDialog();
			}
			if(txtAnimGallery.Text.Length == 0)
			{
				bOk = false;
				WarningDialogue warning = new WarningDialogue("Animation Gallery field must not be empty");
				warning.ShowDialog();
			}
			if(txtBValue.Text.Length == 0)
			{
				txtBValue.Text = "0";
				WarningDialogue warning = new WarningDialogue("Bioenergy Value field must not be empty, auto-filled with 0");
				warning.ShowDialog();
			}
			if(txtAnimString.Text.Length == 0)
			{
				txtAnimString.Text = "0";
				WarningDialogue warning = new WarningDialogue("Animation String field must not be empty, auto-filled with 0");
				warning.ShowDialog();
			}
			if(txtRemove.Text.Length == 0)
			{
				bOk = false;
				WarningDialogue warning = new WarningDialogue("A Remove Script must be present");
				warning.ShowDialog();
			}
			if(treeScripts.Nodes.Count == 0)
			{
				bOk = false;
				WarningDialogue warning = new WarningDialogue("Must have atleast one script file");
				warning.ShowDialog();
			}
			//done
			
			if(bOk)
			{
				bAcceptChanges = true;
				Close();
			}
		}
	}
}
